package com.gjy.learning.scala

import java.sql.{Connection, DriverManager, PreparedStatement}

object OracleTableCreatorWithPreparedStatement {
  val url = "jdbc:oracle:thin:@localhost:1521:ORCL"
  val username = "SCOTT"
  val password = "123456"

  private def createTable(tableName: String, columns: Seq[(String, String)]): Unit = {
    // 加载驱动
    Class.forName("oracle.jdbc.driver.OracleDriver")
    val connection: Connection = DriverManager.getConnection(url, username, password)
    try {
      val columnDefinitions = columns.map { case (columnName, dataType) => s"$columnName $dataType" }.mkString(", ")
      val sql = s"CREATE TABLE $tableName ($columnDefinitions)"
      val preparedStatement: PreparedStatement = connection.prepareStatement(sql)
      preparedStatement.executeUpdate()
      println(s"Table $tableName created successfully.")
    } catch {
      case e: Exception => e.printStackTrace()
    } finally {
      connection.close()
    }
  }

  def main(args: Array[String]): Unit = {
    val tableName = "EMPLOYEES_TEST"
    val columns = Seq(("id", "NUMBER(10) PRIMARY KEY"), ("name", "VARCHAR2(100)"))
    createTable(tableName, columns)
  }

}